package com.yanfan.getui.config;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class GTPushConfig {
    private static volatile GTPushConfig gtPushConfig = null;
    @Value("${uniPush.enabled}")
    private Boolean enabled;
    @Value("${uniPush.appId}")
    private String appId;
    @Value("${uniPush.appKey}")
    private String appKey;
    @Value("${uniPush.masterSecret}")
    private String masterSecret;
    private PushApi pushApi;

    public static GTPushConfig getInstance() {
        if (gtPushConfig == null) {
            synchronized (GTPushConfig.class) {
                if (gtPushConfig == null) {
                    gtPushConfig = new GTPushConfig();
                }
            }
        }
        return gtPushConfig;
    }

    @PostConstruct
    public void apiHelper() {
        gtPushConfig = this;
        if (!enabled) {
            return;
        }
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        // 填写应用配置，参数在“Uni Push”下的“应用配置”页面中获取
        apiConfiguration.setAppId(appId);
        apiConfiguration.setAppKey(appKey);
        apiConfiguration.setMasterSecret(masterSecret);
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        try {
            pushApi = apiHelper.creatApi(PushApi.class);
            System.out.println("个推API创建成功");
        } catch (Exception e) {
            System.out.println("个推API创建失败：" + e.toString());
        }
    }

    /**
     * 发送个推消息
     */
    public Integer sendMessage(String clientId, String title, String message) {
        Integer code = 200;
        if (!enabled) {
            return code;
        }
        try {
            // 根据cid进行单推
            PushDTO<Audience> pushDTO = new PushDTO<Audience>();
            // 设置推送参数，requestid需要每次变化唯一
            pushDTO.setRequestId(System.currentTimeMillis() + "");
            Settings settings = new Settings();
            pushDTO.setSettings(settings);
            // 消息有效期，走厂商消息必须设置该值
            settings.setTtl(3600000);

            // 在线走个推通道时推送的消息体
            PushMessage pushMessage = new PushMessage();
            pushDTO.setPushMessage(pushMessage);
            GTNotification gtNotification = new GTNotification();
            gtNotification.setTitle(title);
            gtNotification.setBody(message);
            gtNotification.setBigText(message);
            gtNotification.setClickType("startapp");
//            gtNotification.setLogoUrl("");
            pushMessage.setNotification(gtNotification);
            // 设置接收人信息
            Audience audience = new Audience();
            pushDTO.setAudience(audience);
            audience.addCid(clientId);
            ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
            if (apiResult.isSuccess()) {
                // success
                System.out.println("个推推送成功：" + apiResult.getData());
            } else {
                // failed
                System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
                code = 500;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return code;
    }
}
